local fuwei = fk.CreateSkill {
  name = "inker_fuwei",
  tags = { Skill.Quest }
}

local DIY = require "packages.diy_utility.diy_utility"

fuwei:addAcquireEffect(function(self, player, is_start)
  local room = player.room
  local logic = room.logic
  logic:getActualDamageEvents(1, function(e)
    local dat = e.data
    if dat.from == player and dat.card then
      player.room:setPlayerMark(player, "@inker_fuwei", dat.card)
      return true
    end
  end)
end)
fuwei:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@inker_fuwei", 0)
end)

fuwei:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(fuwei.name) then
      local damaged = {}
      player.room.logic:getActualDamageEvents(1, function(e)
        local dat = e.data
        if dat.from == player and dat.card then
          table.insert(damaged, dat)
        end
      end, player.HistoryTurn)
      if damaged[1] then
        event:setSkillData(self, "inker_fuwei_damaged", damaged)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local dat = event:getSkillData(self, "inker_fuwei_damaged") ---@type {card:Card, from:Player}[]
    local last_card = player:getMark("@inker_fuwei")
    local diff = false
    if last_card ~= 0 then
      for _, t in ipairs(dat) do
        if t.card:compareColorWith(last_card, true) then
          diff = true
          break
        end
      end
    end
    if diff then
      event:setCostData(self, { succeed = true, anim_type = "big", audio_index = 2 })
    else
      event:setCostData(self, { anim_type = "defensive", audio_index = 1 })
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    if dat.succeed then
      room:changeMaxHp(player, 1)
      room:recover({
        who = player,
        num = 1,
        skillName = fuwei.name,
        recoverBy = player,
      })
      if player.general == "inker__menyue" then
        room:changeHero(player, "inker__guanyue", nil, false, true, false, false)
      elseif player.deputyGeneral == "inker__menyue" then
        room:changeHero(player, "inker__guanyue", nil, false, true, false, false)
      end
    else
      DIY.enterHidden(player)
    end
  end
})
fuwei:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(fuwei.name) and data.card
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@inker_fuwei", data.card)
  end
})

Fk:loadTranslationTable{
  ["inker_fuwei"] = "复威",
  [":inker_fuwei"] = "使命技，当你使用牌造成过伤害的回合结束时，你隐匿。<br>" ..
  "⬤　成功：若此牌与上一次对你造成伤害的牌异色，改为加1点体力上限并回复1点体力，并将此武将牌替换为<a href=':inker__guanyue'>关樾</a>。",

  ["@inker_fuwei"] = "复威",

  ["$inker_fuwei1"] = "国仇家恨，不共戴天！",
  ["$inker_fuwei2"] = "手中虽无青龙吟，心有长刀仍啸月！",
}

return fuwei